using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;
using CampaignWP7.Base;
using Microsoft.Xna.Framework;

namespace CampaignWP7
{
    class Shield : CCSprite
    {
        public float width { set; get; }
        public float height { set; get; }

        public enum ShieldType
        {
            SHIELD_HOLD = 0,
            //
            BT_NUM
        }

        private static string[] ShieldTypeName = { "hold" };
        private static int[] ShieldFrameCount = { 1, 0 };
        protected static Dictionary<string, List<CCSpriteFrame>> m_framesDict;

        public enum ShieldState
        {
            ShieldState_Normal = 0,
            ShieldState_Hold,
            //
            ShieldState_Num
        }

        public ShieldState State { set; get; }
        public CollideBox m_Collide;

        public void MoveBounding(Vector2 pos)
        {
            m_Collide.SetPos(pos);
        }

        public void MoveTo(CCPoint pos)
        {
            this.position = new CCPoint(pos.x, pos.y);
            MoveBounding(new Vector2(pos.x, pos.y));
        }

        public Shield()
        {
            if (m_framesDict == null || m_framesDict.Count == 0)
            {
                m_framesDict = new Dictionary<string, List<CCSpriteFrame>>();
                for (int i = 0; i < (int)ShieldType.BT_NUM; ++i)
                {
                    List<CCSpriteFrame> temp = new List<CCSpriteFrame>();
                    //---
                    //every animation type shouldn't greater then N!
                    //---
                    for (int k = 0; k < ShieldFrameCount[i]; ++k)
                    {
                        string frameName = ShieldTypeName[i];

                        frameName = "shield_" + frameName+ "_" + k + ".png";
                        CCSpriteFrame spriteframe = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(frameName);
                        if (spriteframe != null)
                        {
                            temp.Add(spriteframe);
                        }
                        //
                        if (k == ShieldFrameCount[i] - 1)
                        {
                            if (temp.Count != 0)
                            {
                                string szName = ShieldTypeName[i];
                                m_framesDict.Add(szName, temp);
                            }
                            break;
                        }
                    }
                }
            }

            string initName = "shield_" + ShieldTypeName[0] + "_0.png";
            initWithSpriteFrameName(initName);
            this.anchorPoint = new CCPoint(0.5f, 0.0f);
            //
            ResDef.g_ShieldBatchNode.addChild(this);
            CCSpriteFrame frame = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(initName);
            width = frame.Rect.size.width;
            height = frame.Rect.size.height;
            State = ShieldState.ShieldState_Normal;
            this.anchorPoint = new CCPoint(0.5f, 0.5f);
            m_Collide = new CollideBox(new Vector2(-width/2, height/2), new Vector2(width/2, -height/2));
        }

        public void holdShield()
        {
            State = ShieldState.ShieldState_Hold;
        }

        public bool Intersects(CollideCircle bulletKeypoint, bool justByCenter)
        {
            bool bHit = false;
            if (justByCenter)
            {
                bHit = m_Collide.Intersects(bulletKeypoint.center);
            }
            else
            {
                bHit = m_Collide.Intersects(bulletKeypoint);
            }

            return bHit;
        }
    }
}
